#!/usr/bin/env python
#coding=utf-8
from config import RUN_TRAINING_FILE,FIELD_TYPE_FILE,DISCRET_INDEX_FILE,DISCRETED_FILE
#根据徐方舟源程序改写,付卫平修改，晏小辉修改
# input:读取已去除无价值列的源数据
# output:离散化了的数据

def ReadDataSource(filepath):
    file = open(filepath,"r")
    lines = file.readlines()
    dataSource = []
    #读第一行,获取列数
    lines[0] = lines[0].rstrip()#除去最后一个回车符
    arrColumn = lines[0].split(',')
    #将所有数据读入list
    for i in range( len(arrColumn) ):
        dataSource.append([])#插入一列 
               
    #读取每行
    for i in range(1,len(lines)):
        lines[i] = lines[i].rstrip()
        arr = lines[i].split(',')

        #将第j个元素插入到第j列
        for j in range( len(arrColumn) ):
                dataSource[j].append(arr[j])
        #print i
    return dataSource

#读索引数据
def GetSourceIndex(filepath):
    file = open (filepath,"r")
    lines = file.readlines()#读进了一个列表
    #print lines
    data = ""
    numIndexArray = []
    for i in range(len(lines)):
        lines[i] = lines[i].strip()
        arr = lines[i].split(' ')
        fieldName,fieldType = arr[0],arr[-1]
        numIndexArray.append((i,fieldName,fieldType,i))
    file.close()
    #print numIndexArray
    return numIndexArray

#离散化
def Discretize(dataSource,indexArray,rangeNum,lastIndexFilepath):    
    #建立新的索引文件
    lastIndexFile = open(lastIndexFilepath,"w")
    for i in range(len(dataSource)):        
        if indexArray[i][2] == 'Num' and indexArray[i][1] != 'TARGET_B' and indexArray[i][1] != 'TARGET_D':          
            #print i #第i列
            for j in range(len(dataSource[i])):
                if dataSource[i][j] != '' and dataSource[i][j] != ' ':
                    #print "void"
                    minVal = float(dataSource[i][j])
                    maxVal = float(dataSource[i][j])
                    break
            for j in range(len(dataSource[i])):
                if dataSource[i][j] != '' and dataSource[i][j] != ' ':
                    if minVal > float(dataSource[i][j]):
                        minVal = float(dataSource[i][j])
                    if maxVal < float(dataSource[i][j]):
                        maxVal = float(dataSource[i][j])
            #dataRange.append((minVal,maxVal))#找出数值行最大,最小值
            #写入最后索引文件1列名,2列类型,3分类数,4最小值,5最大值
            lastIndexFile.write(' ' + indexArray[i][1] + ' ' + indexArray[i][2] + ' ' + str(rangeNum) + ' ' + str(minVal) + ' ' + str(maxVal) + ' \n')
            #修正dataSource
            for j in range(len(dataSource[i])):
                if maxVal - minVal == 0:
                    dataSource[i][j] = 0
                else:
                    if dataSource[i][j] != '' and dataSource[i][j] != ' ': 
                        dataSource[i][j] = int((float(dataSource[i][j]) - minVal)/(maxVal - minVal) * rangeNum) 
        else:
            lastIndexFile.write(' ' + indexArray[i][1] + ' ' + indexArray[i][2] + ' \n')            
    return dataSource

def WriteData(filePath,resData,indexArray,lastIndexFilepath):
    #读取索引,并重排序(有必要吗)
    lastindexfile = open(lastIndexFilepath,"r")
    lastindexlines = lastindexfile.readlines()
    lastindexfile.close()
    
    resIndexfile = open(lastIndexFilepath,"w")    
    #重排序:num归一类，char型归一类，target归一类
    num_range = []
    char_range = []
    target_range = []
    for i in range(len(indexArray)):
        if indexArray[i][2] == 'Num' and indexArray[i][1] != 'TARGET_B' and indexArray[i][1] != 'TARGET_D':            
            num_range.append([indexArray[i][0],indexArray[i][1]])
            
        elif indexArray[i][2] == 'Char' and indexArray[i][1] != 'TARGET_B' and indexArray[i][1] != 'TARGET_D':
            char_range.append([indexArray[i][0],indexArray[i][1]])
            #indexArray[i].append(strTemp)
        elif indexArray[i][1] == 'TARGET_B':
            target_range.append([indexArray[i][0],indexArray[i][1]])
            #indexArray[i].append(str("TB"))
        elif indexArray[i][1] == 'TARGET_D':
            target_range.append([indexArray[i][0],indexArray[i][1]])
            #indexArray[i].append(str("TD"))
    
    file_new = open( DISCRETED_FILE,"w")#建立结果文件
           
    d_resData = []#源数据转换
    d_resIndex = []#索引数据转换
    sum = 0
    for i in range(len(num_range)):
        d_resData.append(resData[int(num_range[i][0])])
        d_resIndex.append(lastindexlines[int(num_range[i][0])])
        sum = sum + 1
    for i in range(len(char_range)):
        d_resData.append(resData[int(char_range[i][0])])
        d_resIndex.append(lastindexlines[int(char_range[i][0])])
        sum = sum + 1
    for i in range(len(target_range)):
        d_resData.append(resData[int(target_range[i][0])])
        d_resIndex.append(lastindexlines[int(target_range[i][0])])        
        sum = sum + 1   
    
    #写源数据
    for i in range(len(d_resData[0])):
        for j in range(len(d_resData)-1):
            file_new.write(str(d_resData[j][i]) + ',')
        file_new.write(str(d_resData[len(d_resData)-1][i]) + '\n')
    print "write dataSource file finish:"+DISCRETED_FILE
    
    #print "d_resData"
    #resIndexfile.write("ss")
    #重写索引文件
    for i in range(len(d_resIndex)):
        resIndexfile.write(' ' + str(i+1) + d_resIndex[i])
    print "write index file finish:"+DISCRET_INDEX_FILE
    
if __name__ == "__main__":
    #输入
    # 1 源数据文件名
    filepath = RUN_TRAINING_FILE
    # 2 初始索引文件名    
    indexFilepath = FIELD_TYPE_FILE
    # 3 离散化类别数
    rangeNum = 10
    # 4 最终索引文件名
    lastIndexFilepath = DISCRET_INDEX_FILE  
    #过程
    # 1 读取源数据文件,返回list
    dataSource = ReadDataSource(filepath)
    # 2 读取源索引文件,返回list
    indexArray = GetSourceIndex(indexFilepath)
    # 3 离散化原数据,改变源数据list数值,同时写入最后的索引文件
    #resData = Discretize(dataDelSource,indexnewArray,rangeNum,lastIndexFilepath)
    resData = Discretize( dataSource,indexArray,rangeNum,lastIndexFilepath )
    # 4 重排序源数据,同时写文件,重排序索引文件,同时也写文件
    WriteData( filepath,resData,indexArray,lastIndexFilepath )
